package com.scanner.ui;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.scanner.hardware.R;
import com.scanner.util.SharePreConfig;

public class InputDataScanType extends DialogFragment {
    private String data;
    private int type;
    private EditText etData;

    public static InputDataScanType getInstance(String data,int type){
        InputDataScanType scanType = new InputDataScanType();
        Bundle bundle = new Bundle();
        bundle.putString("data",data);
        bundle.putInt("type",type);
        scanType.setArguments(bundle);
        return scanType;
    }



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if(arguments != null){
            data = arguments.getString("data");
            type = arguments.getInt("type");
        }

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomProgressDialog);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return inflater.inflate(R.layout.dialog_inputdate_layout,container,false);
    }

    @Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TextView tvTitle = view.findViewById(R.id.tv_dialog_title);
        etData = view.findViewById(R.id.et_data);
        if(type == 1){//前缀
            tvTitle.setText(view.getContext().getString(R.string.add_pre));
        }else if(type == 2){//后缀
            tvTitle.setText(view.getContext().getString(R.string.add_back_data_title));
        }

        etData.setText(data);
        etData.setSelection(TextUtils.isEmpty(data) ? 0 : data.length());
        Button btnCancel = view.findViewById(R.id.btn_cancel);
        Button btnSure = view.findViewById(R.id.btn_update);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissDialog();
            }
        });
        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String dates = etData.getText().toString();
                if(type == 1){
                    SharePreConfig.setQianZui(TextUtils.isEmpty(dates) ? "" : dates);
                }else if(type == 2){
                    SharePreConfig.setHouZui(TextUtils.isEmpty(dates) ? "" : dates);
                }
                dissDialog();
            }
        });
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().setCancelable(false);
        getDialog().setCanceledOnTouchOutside(false);
        Window dialogWindow = getDialog().getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(lp);

    }

    public void dissDialog(){
        if(getDialog()!= null && getDialog().isShowing()){
            getDialog().dismiss();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }



}
